/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Operations;

import hep.aida.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author littlechild
 */
public class OpHistogram implements Operation{
    public static final String histogram_mode[]=new String[]{"","Gray","Blue","Red","Green"};
    /**
     * 
     * @param image
     *      the image which the opeartion is performed on
     * @param paras
     *      In this function, this paras must be null.
     * @return
     *      If the operation is done correctly, return the changed BufferedImage
     *      else return null.
     */
    public BufferedImage operate(BufferedImage image, double[] paras) {
        BufferedImage returnValue=null;
        try {
            assert image != null;
            //set mode and compute the histogram
            CmputHistogramInMode cmp = new CmputHistogramInMode();
            cmp.setMode(histogram_mode[(int) paras[0]]);
            IHistogram histogram = cmp.compute(image);
            //accroding to the histogram, get the cooresponding bufferedImage for it 
            IAnalysisFactory af = IAnalysisFactory.create();
            IPlotter plotter = af.createPlotterFactory().create();
            plotter.createRegion().plot(histogram);
            plotter.writeToFile("tmpHistogram.bmp", "bmp");
            returnValue=ImageIO.read(new File("tmpHistogram.bmp"));      
        } catch (IOException ex) {
            Logger.getLogger(OpHistogram.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnValue;
    }
}
